package com.aclass.xqx.classscreen.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aclass.xqx.classscreen.R;
import com.aclass.xqx.classscreen.db.CardStudent;
import com.aclass.xqx.classscreen.ui.StudentPhotosActivity;
import com.aclass.xqx.classscreen.view.XCRoundRectImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import java.util.List;

/**
 * Created by xqx on 2017/10/25.
 * 照片墙
 */

public class StudentToAdapter extends BaseAdapter {
    private Context mContext;
    private List<CardStudent> list;
    public StudentToAdapter(Context mContext, List<CardStudent> list) {
        this.mContext = mContext;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHander viewHander;
        if (convertView==null){
            viewHander=new ViewHander();
            convertView=View.inflate(mContext,R.layout.photos_item,null);
            viewHander.iv= (XCRoundRectImageView) convertView.findViewById(R.id.iv);
            viewHander.tv_name= (TextView) convertView.findViewById(R.id.tv_name);
            viewHander.tv_type= (TextView) convertView.findViewById(R.id.tv_type);
            convertView.setTag(viewHander);

        }else{
            viewHander= (ViewHander) convertView.getTag();
        }
        CardStudent student = list.get(position);
        viewHander.tv_name.setText(student.getStudent_name());
        if (student.getStudent_url().equals("无头像")){
            viewHander.iv.setImageResource(R.mipmap.student);
        }else {
            Glide.with(mContext).load(student.getStudent_url()).asBitmap().listener(new RequestListener<String, Bitmap>() {
                @Override
                public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    notifyDataSetChanged();
                    return false;
                }
            }).into(viewHander.iv);
        }
        if (student.getStudentSign().getSigntype()==1){
            viewHander.tv_type.setVisibility(View.VISIBLE);
        }else{
            viewHander.tv_type.setVisibility(View.GONE);
        }
        return convertView;
    }
    class ViewHander{
        private XCRoundRectImageView iv;
        private TextView tv_name,tv_type;
    }
}
